﻿Public Class FrmGestComm

#Region "champs"
    Dim position As Integer = 0
#End Region

#Region "gestionnaire d'événements"
    Private Sub FrmGestComm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = FrmStart

        'On grise les données
        txtDate.Enabled = False
        txtNum.Enabled = False

        'DataGrid du récapitulatif des commandes
        MySQLConnect.MaRequete = "SELECT idCommande, DateCommande, idCli, NomClient, PrenomClient FROM Commande, Client WHERE Client.idClient = Commande.idCli ORDER BY idCommande ASC"
        MySQLConnect.MaCommandeSpecialeRequete.CommandText = MySQLConnect.MaRequete
        MySQLConnect.MonRemplisseur.SelectCommand = MySQLConnect.MaCommandeSpecialeRequete
        MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "ToutesLesCommandes")
        DGCommandes.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("ToutesLesCommandes")

        'Affichage des données dans le ComboBoxClient
        MySQLConnect.MaRequete = "SELECT * FROM Client"
        MySQLConnect.MaCommandeSpecialeRequete.CommandText = MySQLConnect.MaRequete
        MySQLConnect.MonRemplisseur.SelectCommand = MySQLConnect.MaCommandeSpecialeRequete
        MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "TousLesClients")
        ComboBoxClient.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("TousLesClients")

        ComboBoxClient.DisplayMember = "NomClient"
        ComboBoxClient.ValueMember = "idClient"
        Rafraichir()
        position = 0
    End Sub

    Private Sub DGCommandes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DGCommandes.Click
        txtNum.Text = DGCommandes.CurrentRow.Cells(0).Value()
        txtDate.Text = DGCommandes.CurrentRow.Cells(1).Value()
        ComboBoxClient.SelectedValue = DGCommandes.CurrentRow.Cells(2).Value()
    End Sub

#Region "gestion des boutons"

    Private Sub btnSuivant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuivant.Click
        If position < MySQLConnect.MonJeuDeDonnees.Tables("ToutesLesCommandes").Rows.Count - 1 Then
            position = position + 1
            Rafraichir()
        End If
    End Sub

    Private Sub btnPrecedent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrecedent.Click
        If position > 0 Then
            position = position - 1
            Rafraichir()
        End If
    End Sub

    Private Sub btnPremier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPremier.Click
        If position <> 0 Then
            position = 0
            Rafraichir()
        End If
    End Sub

    Private Sub btnDernier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDernier.Click
        If position <> MySQLConnect.MonJeuDeDonnees.Tables("ToutesLesCommandes").Rows.Count - 1 Then
            position = MySQLConnect.MonJeuDeDonnees.Tables("ToutesLesCommandes").Rows.Count - 1
            Rafraichir()
        End If
    End Sub

    Private Sub btnAjoutComm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjoutComm.Click
        'Gestion de l'ajout d'une commande
        Dim num As Integer
        Dim d As Date

        d = Date.Now.Date
        If btnAjoutComm.Text = "Ajouter une Commande" Then
            txtNum.Text = MySQLConnect.GenererClePrimaire("idCommande", "Commande") '(MySQLConnect.MonJeuDeDonnees.Tables("TousLesClients").Rows.Count) + 1
            txtDate.Text = d

            btnAjoutComm.Text = "Valider !"
        Else
            If txtDate.Text <> "" Then
                'Affectation variables
                num = CInt(txtNum.Text)

                'Requête
                MySQLConnect.MaRequete = "INSERT INTO Commande VALUES (" & num & ", '" & d & "', " & ComboBoxClient.SelectedValue & ")"
                MySQLConnect.MaCommandeSpecialeRequete.CommandText = MySQLConnect.MaRequete
                MySQLConnect.MaCommandeSpecialeRequete.ExecuteNonQuery()

                MessageBox.Show("La commande a été ajoutée", "Ajout d'une commande", MessageBoxButtons.OK)
                position = 0 'On se place sur le premier index

                'On affiche le détail de la commande
                Dim frmLigneComm As New FrmDetailCommande
                frmLigneComm.txtNumCommande.Text = txtNum.Text
                frmLigneComm.txtDateCommande.Text = txtDate.Text
                frmLigneComm.ComboBoxClientComm.Text = ComboBoxClient.Text
                frmLigneComm.Show()

                btnAjoutComm.Text = "Ajouter une Commande"

                RechargerLesDonnees()
                Rafraichir()
            Else
                MessageBox.Show("Veuillez renseignez tous les champs munis d'un '*'", "Erreur !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End If
    End Sub

    Private Sub btnModifComm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifComm.Click
        Dim num As Integer = CInt(txtNum.Text)
        Dim d = txtDate.Text

        btnModifComm.Text = "Valider modification !"

        MySQLConnect.MaRequete = " UPDATE Commande Set idCommande = " & num & ", DateCommande = '" & d & "', idCli = " & ComboBoxClient.ValueMember & ""
        MySQLConnect.MaCommandeSpecialeRequete.CommandText = MySQLConnect.MaRequete
        MySQLConnect.MaCommandeSpecialeRequete.ExecuteNonQuery()

        MessageBox.Show("Commande modifié !", "Modification de commandes", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)

        Rafraichir()
        RechargerLesDonnees()
    End Sub
#End Region

#End Region

#Region "Méthodes et procédures"

    Public Sub Rafraichir()
        txtNum.Text = MySQLConnect.MonJeuDeDonnees.Tables("ToutesLesCommandes").Rows(position).Item(0)
        txtDate.Text = MySQLConnect.MonJeuDeDonnees.Tables("ToutesLesCommandes").Rows(position).Item(1)
        ComboBoxClient.SelectedValue = MySQLConnect.MonJeuDeDonnees.Tables("ToutesLesCommandes").Rows(position).Item(2)
    End Sub

    Public Sub RechargerLesDonnees()
        MySQLConnect.MonJeuDeDonnees.Tables("ToutesLesCommandes").Clear()
        MySQLConnect.MaRequete = "SELECT idCommande, DateCommande, idCli, NomClient, PrenomClient FROM Commande, Client WHERE Client.idClient = Commande.idCli ORDER BY idCommande ASC"
        MySQLConnect.MaCommandeSpecialeRequete.CommandText = MySQLConnect.MaRequete
        MySQLConnect.MonRemplisseur.SelectCommand = MySQLConnect.MaCommandeSpecialeRequete
        MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "ToutesLesCommandes")
        MySQLConnect.MonJeuDeDonnees.Tables("TousLesClients").Clear()

        MySQLConnect.MaRequete = "SELECT * FROM Client"
        MySQLConnect.MaCommandeSpecialeRequete.CommandText = MySQLConnect.MaRequete
        MySQLConnect.MonRemplisseur.SelectCommand = MySQLConnect.MaCommandeSpecialeRequete
        MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "TousLesClients")
    End Sub
#End Region

    Private Sub btnSupprComm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSupprComm.Click
        Dim n As Integer = DGCommandes.CurrentRow.Cells(0).Value()

        Dim result = MessageBox.Show("Etes-vous sur de vouloir supprimer '" & DGCommandes.CurrentRow.Cells(1).Value() & "' ?", "Suppression de Commandes", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If result = DialogResult.Yes Then
            MySQLConnect.MaRequete = "DELETE FROM Commande WHERE idCommande = " & n & ""
            MySQLConnect.MaCommandeSpecialeRequete.CommandText = MySQLConnect.MaRequete
            MySQLConnect.MaCommandeSpecialeRequete.ExecuteNonQuery()

            MessageBox.Show("La commande : '" & DGCommandes.CurrentRow.Cells(1).Value() & "' a ete supprimé !", "Suppresion de commande réussi", MessageBoxButtons.OK, MessageBoxIcon.Information)
            RechargerLesDonnees()
            Rafraichir()
        End If
    End Sub
End Class